package com.myk.game.gobangchess.ai.gravityfour;

public class PointSetting {

    public static Object[] pointSetting(int[][] board, int driction, int turn, int x, int dri_changed) {
        int set_succ = 0;
        int[][] new_board = new int[6][6];
        int new_driction = driction;
        int new_dri_changed = dri_changed;

        for (int i = 0; i < board.length; i++) {
            System.arraycopy(board[i], 0, new_board[i], 0, board[i].length);
        }

        if (x <= 6) {
            if(driction == 10){
                new_board = rotateBoard(new_board, driction - 9);
            }else{
                if (driction == 8) {
                    new_board = rotateBoard(new_board, driction - 5);
                }else{
                    new_board = rotateBoard(new_board, driction - 7);
                }
            }
            if (new_board[0][x - 1] == 0) {
                int maxIdx = findMaxIndex(new_board, x - 1);
                new_board[maxIdx][x - 1] = turn;
                set_succ = 1;
            }
            if(driction == 10){
                new_board = rotateBoard(new_board, 13 - driction);
            }else{
                if(driction == 8){
                    new_board = rotateBoard(new_board, 9 - driction);
                }else{
                    new_board = rotateBoard(new_board, 11- driction);
                }
            }
            new_dri_changed = 0;
        } else {
            if (x != driction && dri_changed == 0) {
                driction = x;
                new_board = rotateBoard(new_board, driction - 7);
                int[][] new_new_board = {{0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0}};
                for (int col = 0; col < 6; col++) {
                    int[] nonZeroElements = extractNonZeroElements(new_board, col);
                    if(x == 7 || x == 9){
                        for (int i = 0; i < nonZeroElements.length; i++) {
                            new_new_board[5-i][col] = nonZeroElements[nonZeroElements.length-i-1];
                        }
                    }else{
                        for (int i = 0; i < nonZeroElements.length; i++) {
                            new_new_board[i][col] = nonZeroElements[i];
                        }
                    }
                }
                new_board = rotateBoard(new_new_board, 11 - driction);
                new_driction = driction;
                set_succ = 1;
                new_dri_changed = 1;
            }
        }

        Object[] result = new Object[4];
        result[0] = set_succ;
        result[1] = new_board;
        result[2] = new_driction;
        result[3] = new_dri_changed;

        return result;
    }

    private static int[][] rotateBoard(int[][] board, int times) {
        int[][] rotatedBoard = board;
        for (int i = 0; i < times; i++) {
            rotatedBoard = rotateClockwise(rotatedBoard);
        }
        return rotatedBoard;
    }

    private static int[][] rotateClockwise(int[][] matrix) {
        int n = matrix.length;
        int[][] rotatedMatrix = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                rotatedMatrix[i][j] = matrix[n - j - 1][i];
            }
        }
        return rotatedMatrix;
    }

    private static int findMaxIndex(int[][] board, int col) {
        for (int i = board.length - 1; i >= 0; i--) {
            if (board[i][col] == 0) {
                return i;
            }
        }
        return -1;
    }

    private static int[] extractNonZeroElements(int[][] board, int col) {
        int[] column = new int[board.length];
        for (int i = 0; i < board.length; i++) {
            column[i] = board[i][col];
        }
        int count = 0;
        for (int num : column) {
            if (num != 0) {
                count++;
            }
        }
        int[] nonZeroElements = new int[count];
        int index = 0;
        for (int num : column) {
            if (num != 0) {
                nonZeroElements[index++] = num;
            }
        }
        return nonZeroElements;
    }
}
